package com.ezgo.erc.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.ezgo.erc.entity.Sysdict;
import com.ezgo.erc.master.exception.ServiceException;
import com.ezgo.erc.master.utils.Constants;
import com.ezgo.erc.master.utils.Page;
import com.ezgo.erc.master.utils.StringUtil;
import com.ezgo.erc.service.SysdictService;
import com.google.common.collect.Maps;

/**
 * 
 * (描述)模块 TODO(系统字典视图控制层)
 * 
 * @author jyx
 * @date 2015年9月1日 下午4:34:27
 *
 */
@Controller
@RequestMapping(value = "/sys/sysdict")
public class SysdictController extends BaseController {
	private Map<String, Object> map = Maps.newHashMap();
	private String tipmess = "";

	@Autowired
	private SysdictService sysdictService;

	@ModelAttribute
	public Sysdict get(@RequestParam(required = false) Long id) {
		if (id != null) {
			return sysdictService.getDict(id);
		} else {
			return new Sysdict();
		}
	}

	@RequestMapping(value = "save")
	public String save(Sysdict sysdict, Model model, RedirectAttributes redirectAttributes) {
		tipmess = sysdict.getDescription() + "【" + sysdict.getDval() + "】";
		try {
			map.clear();
			map.put("dgroup", sysdict.getDgroup());
			map.put("dval", sysdict.getDval());
			List<Sysdict> list = sysdictService.getDictList(map);
			if (list != null && list.size() > 0) {
				addMessage(model, String.format(Constants.PAGE_MESSAGE[0][3], tipmess, tipmess + "已存在，无法添加"));
				return "/sys/sysdictForm";
			} else {
				boolean effectResult = sysdictService.saveDict(sysdict);
				if (effectResult)
					addMessage(redirectAttributes, String.format(Constants.PAGE_MESSAGE[0][0], tipmess));
				else
					addMessage(redirectAttributes, String.format(Constants.PAGE_MESSAGE[0][1], tipmess));
			}
		} catch (ServiceException e) {
			logger.error(String.format(Constants.EX_FORMAT_STR, this.getClass().getName(), e.getMessage()));
			addMessage(redirectAttributes, String.format(Constants.PAGE_MESSAGE[0][2], tipmess, StringUtil.abbr(e.getMessage(), 30)));
		}
		return "redirect:/sys/sysdict/?repage";
	}

	@RequestMapping(value = "delete")
	public String delete(Sysdict sysdict, RedirectAttributes redirectAttributes) {
		tipmess = sysdict.getDescription() + "【" + sysdict.getDval() + "】";
		try {
			boolean effectResult = sysdictService.deleteDict(sysdict.getId());
			if (effectResult)
				addMessage(redirectAttributes, String.format(Constants.PAGE_MESSAGE[1][0], tipmess));
			else
				addMessage(redirectAttributes, String.format(Constants.PAGE_MESSAGE[1][1], tipmess));
		} catch (ServiceException e) {
			logger.error(String.format(Constants.EX_FORMAT_STR, this.getClass().getName(), e.getMessage()));
			addMessage(redirectAttributes, String.format(Constants.PAGE_MESSAGE[1][2], tipmess, StringUtil.abbr(e.getMessage(), 30)));
		}
		return "redirect:/sys/sysdict/?repage";
	}

	@RequestMapping(value = "update")
	public String update(Sysdict sysdict, RedirectAttributes redirectAttributes) {
		tipmess = sysdict.getDescription() + "【" + sysdict.getDval() + "】";
		try {
			boolean effectResult = sysdictService.updateDict(sysdict);
			if (effectResult)
				addMessage(redirectAttributes, String.format(Constants.PAGE_MESSAGE[2][0], tipmess));
			else
				addMessage(redirectAttributes, String.format(Constants.PAGE_MESSAGE[2][1], tipmess));
		} catch (ServiceException e) {
			logger.error(String.format(Constants.EX_FORMAT_STR, this.getClass().getName(), e.getMessage()));
			addMessage(redirectAttributes, String.format(Constants.PAGE_MESSAGE[2][2], tipmess, StringUtil.abbr(e.getMessage(), 30)));
		}
		return "redirect:/sys/sysdict/?repage";
	}

	@RequestMapping(value = { "list", "" })
	public String list(Sysdict sysdict, RedirectAttributes redirectAttributes, HttpServletRequest request, HttpServletResponse response, Model model) {
		try {
			map.clear();
			if (sysdict != null) {
				if (StringUtil.isNotBlank(sysdict.getDkey())) {
					map.put("dkey", sysdict.getDkey() + "");
				}
				if (StringUtil.isNotBlank(sysdict.getDval())) {
					map.put("dval", sysdict.getDval() + "");
				}
				if (StringUtil.isNotBlank(sysdict.getDgroup())) {
					map.put("dgroup", sysdict.getDgroup() + "");
				}
			}
			// List<Map<?, ?>> menuList =
			// sysdictService.getDictMenu(Maps.newHashMap());
			model.addAttribute("page", sysdictService.getDictPage(new Page<Sysdict>(request, response), map));
			// model.addAttribute("menuList", menuList);
		} catch (Exception e) {
			tipmess = sysdict.getDescription() + "【" + sysdict.getDval() + "】";
			logger.error(String.format(Constants.EX_FORMAT_STR, this.getClass().getName(), e.getMessage()));
			addMessage(redirectAttributes, String.format(Constants.PAGE_MESSAGE[3][2], tipmess, StringUtil.abbr(e.getMessage(), 30)));
		}
		return "sys/sysdictList";
	}

	@RequestMapping(value = "view")
	public String view(Sysdict sysdict, RedirectAttributes redirectAttributes, Model model) {
		try {
			model.addAttribute("sysdict", sysdict);
		} catch (Exception e) {
			tipmess = sysdict.getDescription() + "【" + sysdict.getDval() + "】";
			logger.error(String.format(Constants.EX_FORMAT_STR, this.getClass().getName(), e.getMessage()));
			addMessage(redirectAttributes, String.format(Constants.PAGE_MESSAGE[3][2], tipmess, StringUtil.abbr(e.getMessage(), 30)));
			return "redirect:/sys/sysdict/?repage";
		}
		return "sys/sysdictView";
	}

	@RequestMapping(value = "form")
	public String form(Sysdict sysdict, RedirectAttributes redirectAttributes, Model model) {
		try {
			model.addAttribute("sysdict", sysdict);
		} catch (Exception e) {
			tipmess = sysdict.getDescription() + "【" + sysdict.getDval() + "】";
			logger.error(String.format(Constants.EX_FORMAT_STR, this.getClass().getName(), e.getMessage()));
			addMessage(redirectAttributes, String.format(Constants.PAGE_MESSAGE[3][2], tipmess, StringUtil.abbr(e.getMessage(), 30)));
			return "redirect:/sys/sysdict/?repage";
		}
		return "sys/sysdictForm";
	}
}
